import 'package:kkhousekeep/page/login/login_controller.dart';
import 'package:shared_preferences/shared_preferences.dart';

class SpKey {
  static const String accessToken = "accessToken";

  static const String userName = "userName";

  static const String userType = "userType";

  static const String userHeadPortrait = "userHeadPortrait";

  static const String isTest = "isTest";

  static const String roomCheckPass = "roomCheckPass";

  static const String cid = "cid";

  static const String uid = "uid";

  static const String cname = "cname";

  static const String roomInfo = 'roomInfo';

  static const String villageInfo = 'villageInfo';

  static const String relation = "relation";

  static const String ownerName = "ownerName";

  static const String ownerImage = "ownerImage";

  static const String phone = "phone";
}

extension SharedPreferencesExtension on Future<SharedPreferences> {
  Future<String> getAccessToken() async {
    var sp = await this;
    return Future.value(sp.getString(SpKey.accessToken) ?? "");
  }

  Future setAccessToken(String accessToken) async {
    var sp = await this;
    sp.setString(SpKey.accessToken, accessToken);
  }

  Future<String> getUserName() async {
    var sp = await this;
    return Future.value(sp.getString(SpKey.userName) ?? "");
  }

  Future setUserName(String userName) async {
    var sp = await this;
    sp.setString(SpKey.userName, userName);
  }

  Future<UserType> getUserType() async {
    var sp = await this;
    var value = sp.getString(SpKey.userType);
    var userType = UserType.values.firstWhere(
        (element) => element.value == value,
        orElse: () => UserType.tenant);
    return Future.value(userType);
  }

  Future setUserType(UserType userType) async {
    var sp = await this;
    sp.setString(SpKey.userType, userType.value);
  }

  Future<String> getUserHeadPortrait() async {
    var sp = await this;
    return Future.value(sp.getString(SpKey.userHeadPortrait) ?? "");
  }

  Future setUserHeadPortrait(String userHeadPortrait) async {
    var sp = await this;
    sp.setString("userHeadPortrait", userHeadPortrait);
  }

  Future<bool> getIsTest() async {
    var sp = await this;
    return Future.value(sp.getBool(SpKey.isTest) ?? false);
  }

  Future setIsTest(bool isTest) async {
    var sp = await this;
    sp.setBool(SpKey.isTest, isTest);
  }

  Future<bool> getRoomCheckPass() async {
    var sp = await this;
    return Future.value(sp.getBool(SpKey.roomCheckPass) ?? false);
  }

  Future<void> setRoomCheckPass(bool roomCheckPass) async {
    var sp = await this;
    sp.setBool(SpKey.roomCheckPass, roomCheckPass);
  }

  Future<int> getCid() async {
    var sp = await this;
    return Future.value(
      sp.getInt(SpKey.cid),
    );
  }

  Future<void> setCid(int cid) async {
    var sp = await this;
    sp.setInt(SpKey.cid, cid);
  }

  Future<int> getUid() async {
    var sp = await this;
    return Future.value(
      sp.getInt(SpKey.uid)?? -1,
    );
  }

  Future<void> setUid(int uid) async {
    var sp = await this;
    sp.setInt(SpKey.uid, uid);
  }

  Future<String> getCname() async {
    var sp = await this;
    return Future.value(sp.getString(SpKey.cname));
  }

  Future<void> setCname(String cname) async {
    var sp = await this;
    sp.setString(SpKey.cname, cname);
  }

  Future<void> setRelation(String relation) async {
    var sp = await this;
    sp.setString(SpKey.relation, relation);
  }

  Future<String> getRelation() async {
    var sp = await this;
    return Future.value(sp.getString(SpKey.relation) ?? "");
  }

  Future<bool> clear() async {
    var sp = await this;
    var result = await sp.clear();
    return result;
  }

  Future<void> setOwnerName(String name) async {
    var sp = await this;
    sp.setString(SpKey.ownerName, name);
  }

  Future<String> getOwnerName() async {
    var sp = await this;
    return Future.value(sp.getString(SpKey.ownerName) ?? "");
  }

  Future<void> setPhone(String phone) async {
    var sp = await this;
    sp.setString(SpKey.phone, phone);
  }

  Future<String> getPhone() async {
    var sp = await this;
    return Future.value(sp.getString(SpKey.phone) ?? "");
  }
}

Future<SharedPreferences> getSp() async {
  return SharedPreferences.getInstance();
}
